﻿using System;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;

public static class ReflectionExtension
{
    public static readonly BindingFlags InstanceBindingFlags = (BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

    public static T GetCustomAttribute<T>(this MemberInfo memberInfo, bool inherit) where T: Attribute
    {
        return (memberInfo.GetCustomAttributes(typeof(T), inherit).FirstOrDefault<object>() as T);
    }

    public static FieldInfo GetInstanceField(this System.Type type, string fieldName)
    {
        if (string.IsNullOrEmpty(fieldName))
        {
            return null;
        }
        return type.GetField(fieldName, InstanceBindingFlags);
    }

    public static MethodInfo GetInstanceMethod(this System.Type type, string methodName)
    {
        if (string.IsNullOrEmpty(methodName))
        {
            return null;
        }
        return type.GetMethod(methodName, InstanceBindingFlags);
    }
}

